<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\controller;
use app\admin\model\Qinu;
use think\Db;
use think\Request;

class User
{
    /**加载职业列表
     * @param Request $request
     * @return \think\response\View
     */
    public function occupation_list(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('occupation')->where('occupation_name','like',"%$tiaojian%")->limit(10)->select();
            $datarow = Db::name('occupation')->where('occupation_name','like',"%$tiaojian%")->count();
        }else{
            $data = Db::name('occupation')->limit(10)->select();
            $datarow = Db::name('occupation')->count();
        }

        return view('occupation_list',['data'=>$data,'tiaojian'=>$tiaojian,'datarow'=>$datarow]);
    }

    /**加载添加职业添加页面
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add_occupation(Request $request){
        return view('add_occupation');
    }

    /**添加职业
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_occupation(Request $request){
        $occupation_name = $request->param('occupation_name',null);
        if(Db::name('occupation')->insert(['occupation_name'=>$occupation_name])){
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**职业分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function occupation_page(Request $request){
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('occupation')->where('occupation_name','like',"%$tiaojian%")->limit(($page-1)*$limit,$limit)->select();
        }else{
            $data = Db::name('occupation')->limit(($page-1)*$limit,$limit)->select();
        }
        return $data;
    }

    /**删除职业，注意会删除本职业下的所有用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function del_occupation(Request $request){
        $id = $request->post('id',null);
        if(Db::name('occupation')->where('id','in',$id)->delete()){
            Db::name('user')->where('occupation_id','in',$id)->delete();
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**加载编辑职业
     * @param Request $request
     * @return \think\response\View
     */
    public function edit_occupation(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('occupation')->where('id',$id)->find();
        return view('edit_occupation',['data'=>$data]);
    }

    /**更新职业
     * @param Request $request
     * @return \think\response\Json
     */
    public function update_occupation(Request $request){
        $id = $request->param('id', null);
        $occupation_name = $request->param('occupation_name', null);
//        return $id;
        if (Db::name('occupation')->where('id', $id)->update(['occupation_name' =>$occupation_name])) {
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败，你什么都没改变']);
        }
    }
    /**获取权限列表
     * @param Request $request
     * @return \think\response\View
     */
    public function vip_power_list(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('power')
                ->where('power_name','like',"%$tiaojian%")
                ->order('id desc')
                ->limit(10)
                ->select();
            $datarow = Db::name('power')
                ->where('power_name','like',"%$tiaojian%")
                ->count();
        }else{
            $data = Db::name('power')
                ->order('id desc')
                ->limit(10)
                ->select();
            $datarow = Db::name('power')
                ->count();
        }
        return view('vip_power_list',['data'=>$data,'datarow'=>$datarow,'tiaojian'=>$tiaojian]);
    }

    /**加载
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add_vip_power(Request $request){
        return view('add_vip_power');
    }

    /**添加vip权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_vip_power(Request $request){
        $data = $request->post();
        $da = Db::name('power')->where('values',$data['values'])->find();
        if($da){
            return json(['code'=>203,'msg'=>'英文标识已存在']);
        }
        if (Db::name('power')->insert($data)) {
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**权限的分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function vip_power_page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page     = $request->param('page',1);
        $limit    = $request->param('limit',10);
        if($tiaojian){
            $data = Db::name('power')
                ->where('power_name','like',"%$tiaojian%")
                ->order('id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = Db::name('power')
                ->order('id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        return $data;
    }

    /**删除权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function del_vip_power(Request $request){
        $id = $request->param('id',null);
        if(Db::name('power')->where('id','in',$id)->delete()){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**编辑权限
     * @param Request $request
     * @return \think\response\View
     */
    public function edit_vip_power(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('power')
            ->where('id',$id)
            ->find();
        return view('edit_vip_power',['data'=>$data]);
    }

    /**修改权限
     * @param Request $request
     * @return \think\response\Json
     */
    public function update_vip_power(Request $request){
        $data = $request->post();
        if(Db::name('power')->where('id',$data['id'])->update($data)){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败，什么都没改变']);
        }
    }

    /**加载vip列表
     * @param Request $request
     * @return \think\response\View
     */
    public function member(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        if($tiaojian){
            $data = Db::name('vip_role')
                ->where('role_name','like',"%$tiaojian%")
                ->order('id desc')
                ->limit(10)
                ->select();
            $datarow = Db::name('vip_role')
                ->where('role_name','like',"%$tiaojian%")
                ->count();
        }else{
            $data = Db::name('vip_role')
                ->order('id desc')
                ->limit(10)
                ->select();
            $datarow = Db::name('vip_role')
                ->count();
        }
        foreach ($data as $key=>$value){
            if($value['power_id']){
                $array = json_decode($value['power_id'],true);
                $data[$key]['power_id'] = Db::name('power')->where('id','in',$array)->select();
            }
        }
//        echo '<pre>';
//        print_r($data);exit;
        return view('member',['data'=>$data,'tiaojian'=>$tiaojian,'datarow'=>$datarow]);
    }

    /**加载添加vip
     * @param Request $request
     * @return \think\response\View
     */
    public function show_add_member(Request $request){
        $power = Db::name('power')->select();
        return view('add_member',['power'=>$power]);

    }

    /**添加vip
     * @param Request $request
     * @return \think\response\Json
     */
    public function add_member(Request $request){
        $data = $request->post();
        if(!isset($data['power_id'])){
            return json(['code'=>203,'msg'=>'请选择权限']);
        }
        if($data['power_id']){
            $data['power_id'] = json_encode($data['power_id']);
        }
        $file = $request->file();
        if(isset($file['biaozhi'])){
            $file = Qinu::Upfile($file['biaozhi']);
            if($file){
                $data['biaozhi'] = $file;
            }
        }
        if (Db::name('vip_role')->insert($data)) {
            return json(['code'=>200,'msg'=>'添加成功']);
        }else{
            return json(['code'=>203,'msg'=>'添加失败']);
        }
    }

    /**删除vip
     * @param Request $request
     * @return \think\response\Json
     */
    public function del_member(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('vip_role')->where('id','in',$id)->select();
        if(Db::name('vip_role')->where('id','in',$id)->delete()){
            foreach ($data as $value){
                if($value['biaozhi']){
                    Qinu::DeleFile($value['biaozhi']);
                }
            }
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>203,'msg'=>'删除失败']);
        }
    }

    /**vip分页
     * @param Request $request
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function member_page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page     = $request->param('page',1);
        $limit    = $request->param('limit',10);
        if($tiaojian){
            $data = Db::name('vip_role')
                ->where('role_name','like',"%$tiaojian%")
                ->order('id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = Db::name('vip_role')
                ->order('id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        foreach ($data as $key=>$value){
            if($value['power_id']){
                $array = json_decode($value['power_id'],true);
                $array = Db::name('power')->where('id','in',$array)->select();
                if($array){
                    $data[$key]['power_id'] = [];
                    foreach ($array as $va){
                        $data[$key]['power_id'][] = $va['power_name'];
                    }
                }
            }
        }
        return $data;
    }

    /**编辑vip
     * @param Request $request
     * @return \think\response\View
     */
    public function edit_member(Request $request){
        $id = $request->param('id',null);
        $data = Db::name('vip_role')->where('id',$id)->find();
        $data['power_id'] = json_decode($data['power_id']);
        $power = Db::name('power')->select();
        return view('edit_member',['data'=>$data,'power'=>$power]);
    }

    /**修改vip设置
     * @param Request $request
     * @return \think\response\Json
     */
    public function update_member(Request $request){
        $data = $request->post();
        if(isset($data['power_id'])){
            $data['power_id'] = json_encode($data['power_id']);
        }
        $file = $request->file();
        if(isset($file['biaozhi'])){
            $file = Qinu::Upfile($file['biaozhi']);
            if($file){
                $data['biaozhi'] = $file;
                $array = Db::name('vip_role')->where('id',$data['id'])->find();
                if($array['biaozhi']){
                    Qinu::DeleFile($array['biaozhi']);
                }
            }
        }
        if (Db::name('vip_role')->where('id',$data['id'])->update($data)) {
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败，你什么都没改变']);
        }
    }

    /**查看用户列表
     * @param Request $request
     * @return \think\response\View
     */
    public function user_list(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        if($tiaojian){
            $data = \app\admin\model\User::get()
                ->where('user.user_name|user.nickname|vip_role.role_name|occupation.occupation_name','like',"%$tiaojian%")
                ->order('user.role_id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
            $datarow = \app\admin\model\User::get()
                ->where('user.user_name|user.nickname|vip_role.role_name|occupation.occupation_name','like',"%$tiaojian%")
                ->count();
        }else{
            $data = \app\admin\model\User::get()
                ->order('user.role_id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
            $datarow = \app\admin\model\User::get()
                ->count();
        }
        return view('user_list',['data'=>$data,'datarow'=>$datarow,'tiaojian'=>$tiaojian]);
    }

    /**启用禁用用户
     * @param Request $request
     * @return \think\response\Json
     */
    public function update_user(Request $request){
        $id = $request->param('id',null);
        $status = $request->param('status',null);
        if(Db::name('user')->where('id',$id)->update(['status'=>$status])){
            return json(['code'=>200,'msg'=>'修改成功']);
        }else{
            return json(['code'=>203,'msg'=>'修改失败,你什么都没改变']);
        }
    }

    /**用户分页
     * @param Request $request
     * @return mixed
     */
    public function user_page(Request $request){
        $tiaojian = $request->param('tiaojian',null);
        $page = $request->param('page',1);
        $limit = $request->param('limit',10);
        if($tiaojian){
            $data = \app\admin\model\User::get()
                ->where('user.user_name|user.nickname|vip_role.role_name|occupation.occupation_name','like',"%$tiaojian%")
                ->order('user.role_id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }else{
            $data = \app\admin\model\User::get()
                ->order('user.role_id desc')
                ->limit(($page-1)*$limit,$limit)
                ->select();
        }
        foreach ($data as $key=>$value){
            $data[$key]['birthday'] = date('Y-m-d',$value['birthday']);
        }
        return $data;
    }
}